using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Web.Services;
using System.Text.RegularExpressions;

public partial class Pages_Employee_ManageVPPPayment_AddVPPPayment : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!Page.IsPostBack)
        {
            Session["VPPID"] = null;
        }
    }

    protected void btnSubmit_Click(object sender, EventArgs e)
    {
        lblMessage.Text = "";
        lnkVPPInvoice.Text = "";
        lnkVPPInvoice.Visible = false;
        if (Page.IsValid)
        {
            try
            {
                int deliverableID = Convert.ToInt32(txtDeliverableID.Text);
                decimal amount = Convert.ToDecimal(txtAmount.Text);
                decimal charge = Convert.ToDecimal(txtCharge.Text);
                int result = DALVPPPayment.InsertVPPPayment(deliverableID, amount, charge,cboIsCustomerPaid.Checked);
                if (result > 0 )
                {
                    lblMessage.Text = "VPP Payment added successfully.";
                    lnkVPPInvoice.Text = "View VPP Invoice";
                    lnkVPPInvoice.Visible = true;
                    DisableAllFields();
                    Session["VPPID"] = deliverableID;
                }
                else
                {
                    lblMessage.Text = "Error adding new VPP Payment. Please note that deliverable ID must exists and cannot be duplicated in VPP Payments.";
                }
            }
            catch
            {
                lblMessage.Text = "Error adding new VPP Payment. Please note that deliverable ID must exists and cannot be duplicated in VPP Payments.";
            }
        }
    }

    protected void custAmount_ServerValidate(object source, ServerValidateEventArgs args)
    {
        try
        {
            decimal amount = Convert.ToDecimal(args.Value);
            args.IsValid = (amount > 0);
        }
        catch
        {
            args.IsValid = false;
        }
    }

    protected void custCharge_ServerValidate(object source, ServerValidateEventArgs args)
    {
        try
        {
            decimal charge = Convert.ToDecimal(args.Value);
            args.IsValid = (charge > 0);
        }
        catch
        {
            args.IsValid = false;
        }
    }

    protected void DisableAllFields()
    {
        txtDeliverableID.ReadOnly = true;
        txtCharge.ReadOnly = true;
        txtAmount.ReadOnly = true;
        cboIsCustomerPaid.Enabled = false;
        btnSubmit.Enabled = false;       
    }


    /// <summary>
    /// This webmethod checks whether an deliverable ID is existed
    /// </summary>
    /// <param name="id">id</param>
    /// <returns>1 if the parameter is an id of an existed deliverable, 0 otherwise, -1 if id string is not a valid id</returns>
    [WebMethod]
    public static int IsExistedID(string id)
    {
        Regex idPattern = new Regex("^[1-9][0-9]*$");
        if (!idPattern.IsMatch(id))
            return -1;
        int n = Int32.Parse(id);
        if (DALDeliverable.GetDeliverablesByDeliverableID(n).Count != 0)
            return 1;
        else
            return 0;
    }

   
}
